from socket import *
import time
import sys



def sender(s):
      while True:
            msg_from_server=s.recv(1024)
            command_server=msg_from_server.decode()
            if command_server =='at':
                  chose=input('Атакуем!!  1-голова; 2-туловище;3-ноги\n\t')
                  if chose=='1':
                        s.send(b'1')
                  elif chose=='2':
                        s.send(b'2')
                  elif chose=='3':
                        s.send(b'3')
                  else:print('error_at')
            elif command_server =='def':
                  chose=input('Защищаемся!!  1-голова; 2-туловище;3-ноги\n\t')
                  if chose=='1':
                        s.send(b'1')
                  elif chose=='2':
                        s.send(b'2')
                  elif chose=='3':
                        s.send(b'3')
                  else:print('error_def')
            elif command_server =='Game Over':
                  break
            elif command_server == None:
                  break
            else: print(command_server)

s=socket(AF_INET, SOCK_STREAM)  
s.connect(('localhost',50000))
text=input('\nEnter your name\n')
while True:
      s.send(text.encode())#1
      data=s.recv(1024)
      print(data.decode())
      while True:
            def mess():
                  mess=input('1-online; 2-enter the battle; 3-battle vs comp; 4-disc\n\t')
                  if mess=='1':
                        return '1'
                  elif mess=='2':
                        return '2'
                  elif mess=='3':
                        return '3'
                  elif mess=='4':
                        return'4'
                  else:
                        print('error. try again')
                        mess()
            sendmess=mess()
            if sendmess=='1':
                  s.send(b'online')
            if sendmess=='2':
                  s.send(b'battle vs player')
            if sendmess=='3':
                  s.send(b'battle vs comp')
            if sendmess=='4':
                  s.send(b'disc')
                  s.close()
                  sys.exit(0)
            data1=s.recv(1024)
            data1=data1.decode()
            if data1==('okPvE'):
                  print('Waiting for battlle')
                  sender(s)
            else:print(data1)

            
      
s.close()
